﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using AIFSCMSDataAccessLayer.Dao;

using AIFSContentManagementSystem.DomainObjects;
using AIFSContentManagementSystem.DomainObjects.Mapper;

using AIFSContentManagementSystem.CriteriaManagement.Messages;

namespace AIFSContentManagementSystem.CriteriaManagement
{
    public class CriteriaManager : ICriteriaManager
    {
        public GetCriteriaResponse GetCriteria(Messages.GetCriteriaRequest request)
        {
            GetCriteriaResponse response = new GetCriteriaResponse();
            
            if (!request.CriteriaId.HasValue)
                response.AddErrorMessage("Criteria id is required");

            DataTable dtCriteria = this.CriteriaDao.GetCriteria(request.CriteriaId);

            if (dtCriteria != null && dtCriteria.Rows.Count != 0)
            {
                CriteriaMapper criteriaMapper = new CriteriaMapper(this.CriterionExpressionFactory);
                Criteria criteria = criteriaMapper.Map(dtCriteria.Rows[0]);
                response.AddCriterion(criteria);
            }

            return response;
        }

        #region ICriteriaManager Members

        IManagerContext managerContext;
        public IManagerContext ManagerContext
        {
            get
            {
                return managerContext;
            }
            set
            {
                managerContext = value;
            }
        }

        ICriteriaDao criteriaDao;
        public ICriteriaDao CriteriaDao
        {
            get
            {
                return criteriaDao;
            }
            set
            {
                criteriaDao = value;
            }
        }

        ICriterionExpressionFactory criterionExpressionFactory;
        
        public ICriterionExpressionFactory CriterionExpressionFactory
        {
            get
            {
                return criterionExpressionFactory;
            }
            set
            {
                criterionExpressionFactory = value;
            }
        }

        ICriteriaAsserter criteriaAsserter;

        public ICriteriaAsserter CriteriaAsserter
        {
            get
            {
                return criteriaAsserter;
            }
            set
            {
                criteriaAsserter = value;
            }
        }

        #endregion
    }
}
